package com.example.zhanghehe.myapplication.opengl3

import android.content.Context
import android.opengl.GLSurfaceView
import android.view.MotionEvent

class OpenGl3SurfaceView(context: Context): GLSurfaceView(context) {

    private var myRender: OpenGl3Render

    init {
        setEGLContextClientVersion(3)
        super.setEGLConfigChooser(8,8,8,8,16,0)
        myRender=OpenGl3Render(context)
        setRenderer(myRender)
        renderMode= RENDERMODE_CONTINUOUSLY

    }


    private var mPreviousX: Float=0f
    private var mPreviousY: Float=0f
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        val x=event!!.getX()
        val y=event!!.getY()

        when (event?.action) {
            MotionEvent.ACTION_MOVE -> {
                var dx=x - mPreviousX
                myRender.setX(myRender.getX()-(dx*TOUCH_SCALE_FACTOR))
                val dy=y-mPreviousY
                myRender.setY(myRender.getY()-(dy*TOUCH_SCALE_FACTOR))
            }
            else -> {
            }
        }
        mPreviousX=x
        mPreviousY=y
        return true
    }

    companion object {
        val TOUCH_SCALE_FACTOR=0.015f
    }

}